package org.springblade.tms.config;

import org.springblade.tms.module.inquirysheet.base.InquirySheetService;
import org.springblade.tms.module.inquirysheet.service.IInquirySheetService;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static org.springblade.tms.global.constant.RedisConstant.INQUIRY_FORM_VOID;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.config
 * @className: RedisKeyExpirationListener
 * @author: yr
 * @description: rediskey失效监听器
 * @date: 2022/5/10 16:24
 */
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {


	public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
		super(listenerContainer);
	}

	@Resource
	private InquirySheetService inquirySheetService;

	/**
	 * 针对redis数据失效事件，进行数据处理
	 *
	 * @param message
	 * @param pattern
	 */
	@Override
	public void onMessage(Message message, byte[] pattern) {
		// 用户做自己的业务处理即可,注意message.toString()可以获取失效的key
		String expiredKey = message.toString();
		//过期处理
		if (expiredKey.contains(INQUIRY_FORM_VOID)) {
			inquirySheetService.expired(expiredKey);
		}
	}
}
